﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.Utils.Menu;
using DevExpress.XtraBars;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraReports.Serialization;
using DevExpress.XtraTabbedMdi;
using ERP_RORZE_PRODUCT.Product;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using ERP_RORZE_DLL;
using ERP_RORZE_ORDER.Common;
using ERP_RORZE_ORDER.OrderObject.UserControls;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_PERMISSION.Forms;
using ERP_RORZE_PERMISSION.UserControls;
//using ERP_RORZE_PRODUCT.Product;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Tool.BCThoigianlamviecthang;
using ERP_RORZE_TOOL.Tool.BaoCaoKeHoach;
using ERP_RORZE_TOOL.Tool.CheckThoiGianLamViec;
using ERP_RORZE_TOOL.Tool.Forms;
using ERP_RORZE_TOOL.Tool.NGNoibo;
using ERP_RORZE_TOOL.Tool.UserControls;
using Utilities = ERP_RORZE_TOOL.Common.Utilities;

namespace ERP_RORZE
{
    public partial class MainForm : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private bool businessLoad;
        private bool assemblyLoad;
        private bool systemLoad;
        private ERP_RORZE_DLL.Business.BusinessPanel businessPanel;
        private ERP_RORZE_DLL.Assembly.AssemblyPanel assemblyPanel;
        private ERP_RORZE_DLL.BasicList.BasicListPanel basiclistPanel;

        private INISettings mySetting;
        private SqlConnection myConnection;

        private LogUtils log;
        private Permission role;
        private string _userName;
        //Tool cutting
        private GridView gridView;

        public MainForm()
        {
            InitializeComponent();
            AddStyleList();
            log = new LogUtils();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);

            businessLoad = false;
            assemblyLoad = false;
            systemLoad = false;

            frm_Login login_Form = new frm_Login();
            login_Form.ShowDialog();
            this.role = new Permission();
            if (role.Is_Login())
            {
                this._userName = role.UserName;
                CallSystemPanel();
                InitToolControls();
                ShowHideLink();
            }
            else
            {

                //if (role.Is_Login())
                //{
                //    CallSystemPanel();
                //    InitToolControls();
                //    ShowHideLink();
                //}
                //else
                //{
                //    this.Dispose();
                //}
                this.Dispose();
            }
        }

        private void ribbonControl1_SelectedPageChanged(object sender, EventArgs e)
        {
            switch (ribbonControl1.SelectedPage.Name)
            {
                case "businessTab":
                    CallBusinessPanel();
                    break;

                case "assemblyTab":
                    CallAssemblyPanel();
                    break;

                case "systemTab":
                    CallSystemPanel();
                    break;

                case "rpProduct":
                    systemLoad = false;
                    role.Is_Login();
                    if (role.HasRight(PermissionConstants.Functions.Tab.ToolCutting))
                    {
                        rpCatProducTool.Visible = true;
                    }
                    else
                    {
                        rpCatProducTool.Visible = false;
                    }

                    if (role.HasRight(PermissionConstants.Functions.Tab.F2Order))
                    {
                        rpCatOrder.Visible = true;
                    }
                    else
                    {
                        rpCatOrder.Visible = false;
                    }

                    ctrl_ProductMain frPro = new ctrl_ProductMain();
                    panelMain.Controls.Clear();
                    panelMain.Controls.Add(frPro);
                    frPro.Dock = DockStyle.Fill;
                    break;

                case "rpToolCutting":
                    systemLoad = false;

                    rpCatProducTool.Visible = true;
                    rpCatOrder.Visible = false;
                    panelMain.Controls.Clear();

                    break;

                case "rpOrder":
                    systemLoad = false;
                    rpCatProducTool.Visible = false;
                    rpCatOrder.Visible = true;

                    ucOrderMain frOrder = new ucOrderMain();
                    panelMain.Controls.Clear();
                    panelMain.Controls.Add(frOrder);
                    frOrder.Dock = DockStyle.Fill;
                    break;

                default:
                    this.panelMain.Controls.Clear();
                    break;
            }

            //Product
            if (ribbonControl1.SelectedPage == businessTab || ribbonControl1.SelectedPage == assemblyTab || ribbonControl1.SelectedPage == systemTab)
            {
                rpCatOrder.Visible = false;
                rpCatProducTool.Visible = false;
            }
            //end Product 

        }

        private void barButtonItem27_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DatabaseConnection DatabaseConnectionForm = new DatabaseConnection();
            DatabaseConnectionForm.ShowDialog();

        }


        ///////////////////////////
        /// Call System Panel
        //////////////////////////
        private void CallSystemPanel()
        {
            if (!systemLoad)
            {
                this.basiclistPanel = new ERP_RORZE_DLL.BasicList.BasicListPanel();
                systemLoad = true;

            }
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(this.basiclistPanel);
            // 
            // systemPanel
            // 
            this.basiclistPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.basiclistPanel.Location = new System.Drawing.Point(2, 2);
            this.basiclistPanel.Name = "basiclistPanel";
            this.basiclistPanel.Size = new System.Drawing.Size(934, 286);
            this.basiclistPanel.TabIndex = 0;
        }

        ///////////////////////////
        /// Call Business Panel
        //////////////////////////
        private void CallBusinessPanel()
        {
            if (!businessLoad)
            {
                this.businessPanel = new ERP_RORZE_DLL.Business.BusinessPanel();
                businessLoad = true;
            }
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(this.businessPanel);
            // 
            // businessPanel
            // 
            this.businessPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.businessPanel.Location = new System.Drawing.Point(2, 2);
            this.businessPanel.Name = "businessPanel";
            this.businessPanel.Size = new System.Drawing.Size(934, 286);
            this.businessPanel.TabIndex = 0;
        }

        ///////////////////////////
        /// Call Assembly Panel
        //////////////////////////
        private void CallAssemblyPanel()
        {
            if (!assemblyLoad)
            {
                this.assemblyPanel = new ERP_RORZE_DLL.Assembly.AssemblyPanel();
                assemblyLoad = true;
            }
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(this.assemblyPanel);
            // 
            // assemblyPanel
            // 
            this.assemblyPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.assemblyPanel.Location = new System.Drawing.Point(2, 2);
            this.assemblyPanel.Name = "assemblyPanel";
            this.assemblyPanel.Size = new System.Drawing.Size(934, 286);
            this.assemblyPanel.TabIndex = 0;
        }

        private void barImportQuotation_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        #region tool cutting

        private void bbtImport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            Form fr = new frm_ImportAdd();
            if (!Utilities.FormUtil.IsFormOpen(fr))
            {
                fr.StartPosition = FormStartPosition.CenterScreen;
                fr.Activate();
                fr.Show();
            }
            else
            {
                Utilities.FormUtil.ActiveForm(fr);
            }

            Cursor.Current = Cursors.Default;
        }

        private void bbtJustNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            Form fr = new frm_JustNewAdd();
            if (!Utilities.FormUtil.IsFormOpen(fr))
            {
                fr.StartPosition = FormStartPosition.CenterScreen;
                fr.Activate();
                fr.Show();
            }
            else
            {
                Utilities.FormUtil.ActiveForm(fr);
            }

            Cursor.Current = Cursors.Default;
        }

        private void bbtBorrow_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            Form fr = new frm_BorrowAdd();
            if (!Utilities.FormUtil.IsFormOpen(fr))
            {
                fr.StartPosition = FormStartPosition.CenterScreen;
                fr.Activate();
                fr.Show();
            }
            else
            {
                Utilities.FormUtil.ActiveForm(fr);
            }

            Cursor.Current = Cursors.Default;
        }

        private void bbtBroken_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            Form fr = new frm_BrokenAdd();
            if (!Utilities.FormUtil.IsFormOpen(fr))
            {
                fr.StartPosition = FormStartPosition.CenterScreen;
                fr.Activate();
                fr.Show();
            }
            else
            {
                Utilities.FormUtil.ActiveForm(fr);
            }

            Cursor.Current = Cursors.Default;
        }

        private void bbtReturn_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            Form fr = new frm_ReturnAdd();
            if (!Utilities.FormUtil.IsFormOpen(fr))
            {
                fr.StartPosition = FormStartPosition.CenterScreen;
                fr.Activate();
                fr.Show();
            }
            else
            {
                Utilities.FormUtil.ActiveForm(fr);
            }

            Cursor.Current = Cursors.Default;
        }

        ////////////////////
        ///report
        //////////////////
        private void subImportDetails_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            //uc_rpt_Tool rp = new uc_rpt_Tool(ToolConstants.Configuration.ReportName.FImportDetails, subImportDetails.Caption);
            //this.panelMain.Controls.Clear();
            //this.panelMain.Controls.Add(rp);
            //rp.Dock = DockStyle.Fill;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.FImportDetails, subImportDetails.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subMonthlyImport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            //uc_rpt_Tool rp = new uc_rpt_Tool(ToolConstants.Configuration.ReportName.FMonthlyImport, subMonthlyImport.Caption);
            //this.panelMain.Controls.Clear();
            //this.panelMain.Controls.Add(rp);
            //rp.Dock = DockStyle.Fill;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.FMonthlyImport, subMonthlyImport.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subUseDetails_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            //uc_rpt_Tool rp = new uc_rpt_Tool(ToolConstants.Configuration.ReportName.FUseDetails, subUseDetails.Caption);
            //this.panelMain.Controls.Clear();
            //this.panelMain.Controls.Add(rp);
            //rp.Dock = DockStyle.Fill;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.FUseDetails, subUseDetails.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subNewOldDetails_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            //uc_rpt_Tool rp = new uc_rpt_Tool(ToolConstants.Configuration.ReportName.FNewOldDetails, subNewOldDetails.Caption);
            //this.panelMain.Controls.Clear();
            //this.panelMain.Controls.Add(rp);
            //rp.Dock = DockStyle.Fill;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.FNewOldDetails, subNewOldDetails.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subBorrowDetails_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            //uc_rpt_Tool rp = new uc_rpt_Tool(ToolConstants.Configuration.ReportName.FBorrowDetails, subBorrowDetails.Caption);
            //this.panelMain.Controls.Clear();
            //this.panelMain.Controls.Add(rp);
            //rp.Dock = DockStyle.Fill;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.FBorrowDetails, subBorrowDetails.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subTotalToolAtFactoryBorrowAtPresent_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            //uc_rpt_Tool rp = new uc_rpt_Tool(ToolConstants.Configuration.ReportName.FTotalMachineAtFactoryBorrowAtPresent, subTotalToolAtFactoryBorrowAtPresent.Caption);
            //this.panelMain.Controls.Clear();
            //this.panelMain.Controls.Add(rp);
            //rp.Dock = DockStyle.Fill;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.FTotalMachineAtFactoryBorrowAtPresent, subTotalToolAtFactoryBorrowAtPresent.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subTotalMachineAtFactoryCrosstabBorrow_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.FTotalMachineAtFactoryCrosstabBorrow, subTotalMachineAtFactoryCrosstabBorrow.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subTotalMachineAtFactoryBorrow_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            //uc_rpt_Tool rp = new uc_rpt_Tool(ToolConstants.Configuration.ReportName.FTotalMachineAtFactoryBorrow, subTotalMachineAtFactoryBorrow.Caption);
            //this.panelMain.Controls.Clear();
            //this.panelMain.Controls.Add(rp);
            //rp.Dock = DockStyle.Fill;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.FTotalMachineAtFactoryBorrow, subTotalMachineAtFactoryBorrow.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;
            Cursor.Current = Cursors.Default;
        }

        private void subTonKhoTongHop_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.FTonKhoTongHop, subTonKhoTongHop.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;

            this.gridView = rp.GridView;
            Cursor.Current = Cursors.Default;
        }

        private void subQTotalInstock1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            //uc_rpt_Tool rp = new uc_rpt_Tool(ToolConstants.Configuration.ReportName.QTotalInstock1, subQTotalInstock1.Caption);
            //this.panelMain.Controls.Clear();
            //this.panelMain.Controls.Add(rp);
            //rp.Dock = DockStyle.Fill;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QTotalInstock1, subQTotalInstock1.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;
            Cursor.Current = Cursors.Default;
        }

        private void subQTotalInStockAtPresent_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            //uc_rpt_Tool rp = new uc_rpt_Tool(ToolConstants.Configuration.ReportName.QTotalInStockAtPresent, subQTotalInStockAtPresent.Caption);
            //this.panelMain.Controls.Clear();
            //this.panelMain.Controls.Add(rp);
            //rp.Dock = DockStyle.Fill;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QTotalInStockAtPresent, subQTotalInStockAtPresent.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;

            this.gridView = rp.GridView;
            Cursor.Current = Cursors.Default;
        }

        private void subQMonthlyInStockQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QMonthlyInStock_Query, subQMonthlyInStockQuery.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subQMonthlyInStockQueryQuantity_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QMonthlyInStock, subQMonthlyInStockQueryQuantity.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subFReturnDetails_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            //uc_rpt_Tool rp = new uc_rpt_Tool(ToolConstants.Configuration.ReportName.FReturnDetails, subFReturnDetails.Caption);
            //this.panelMain.Controls.Clear();
            //this.panelMain.Controls.Add(rp);
            //rp.Dock = DockStyle.Fill;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.FReturnDetails, subFReturnDetails.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subFLossDetails_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.FLossDetails, subFLossDetails.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subFDailyBroken_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.FDailyBroken, subFDailyBroken.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subQMonthlyBroken2CrosstabAmountUSD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QMonthlyBroken2CrosstabAmountUSD, subQMonthlyBroken2CrosstabAmountUSD.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subQMonthlyBroken2CrosstabQty_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QMonthlyBroken2CrosstabQty, subQMonthlyBroken2CrosstabQty.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subQMonthlyBrokenMachineCrosstabAmountUSD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QMonthlyBrokenMachineCrosstabAmountUSD, subQMonthlyBrokenMachineCrosstabAmountUSD.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subQMonthlyBrokenMachineCrosstabQty_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QMonthlyBrokenMachineCrosstabQty, subQMonthlyBrokenMachineCrosstabQty.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subQMonthlyBrokenCustomer2CrosstabQty_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QMonthlyBrokenCustomer2CrosstabQty, subQMonthlyBrokenCustomer2CrosstabQty.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subQMonthlyBrokenCustomer2CrosstabAmountUSD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QMonthlyBrokenCustomer2CrosstabAmountUSD, subQMonthlyBrokenCustomer2CrosstabAmountUSD.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subQMonthlyBroken3CrosstabToolGroupAmount_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QMonthlyBroken3CrosstabToolGroupAmount, subQMonthlyBroken3CrosstabToolGroupAmount.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subQMonthlyBroken3CrosstabToolGroupQty_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QMonthlyBroken3CrosstabToolGroupQty, subQMonthlyBroken3CrosstabToolGroupQty.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subQMonthlyBrokenMaterial2CrosstabAmountUSD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QMonthlyBrokenMaterial2CrosstabAmountUSD, subQMonthlyBrokenMaterial2CrosstabAmountUSD.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subQMonthlyBrokenMaterial2CrosstabQty_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QMonthlyBrokenMaterial2CrosstabQty, subQMonthlyBrokenMaterial2CrosstabQty.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subQMonthlyBrokenMistake2CrosstabAmountUSD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QMonthlyBrokenMistake2CrosstabAmountUSD, subQMonthlyBrokenMistake2CrosstabAmountUSD.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subQMonthlyBrokenMistake2CrosstabQty_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QMonthlyBrokenMistake2CrosstabQty, subQMonthlyBrokenMistake2CrosstabQty.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void subQTotalMachineBrokenCrosstab_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            uc_rpt_GridView rp = new uc_rpt_GridView(ToolConstants.Configuration.ReportName.QTotalMachineBrokenCrosstab, subQTotalMachineBrokenCrosstab.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(rp);
            rp.Dock = DockStyle.Fill;
            this.gridView = rp.GridView;

            Cursor.Current = Cursors.Default;
        }

        private void bbtExportSelected_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Utilities.ExportToExcel(gridView, 0, true);
        }

        private void bbtExportAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Utilities.ExportToExcel(gridView, 1, true);

        }

        private void InitToolControls()
        {
            PopupMenu popupMenuImport = new PopupMenu();
            PopupMenu popupMenuJustNew = new PopupMenu();
            PopupMenu popupMenuBorrow = new PopupMenu();
            PopupMenu popupMenuBroken = new PopupMenu();
            PopupMenu popupMenuReturn = new PopupMenu();

            bbItemImport.ItemClick += bbItemImport_ItemClick;
            bbItemJustnew.ItemClick += bbItemJustnew_ItemClick;
            bbItemBorrow.ItemClick += bbItemBorrow_ItemClick;
            bbItemBroken.ItemClick += bbItemBroken_ItemClick;
            bbItemReturn.ItemClick += bbItemReturn_ItemClick;

            popupMenuImport.ItemLinks.Add(bbItemImport);
            popupMenuJustNew.ItemLinks.Add(bbItemJustnew);
            popupMenuBorrow.ItemLinks.Add(bbItemBorrow);
            popupMenuBroken.ItemLinks.Add(bbItemBroken);
            popupMenuReturn.ItemLinks.Add(bbItemReturn);

            bbtImport.DropDownControl = popupMenuImport;
            bbtBorrow.DropDownControl = popupMenuBorrow;
            bbtJustNew.DropDownControl = popupMenuJustNew;
            bbtBroken.DropDownControl = popupMenuBroken;
            bbtReturn.DropDownControl = popupMenuReturn;

        }

        void bbItemReturn_ItemClick(object sender, ItemClickEventArgs e)
        {

            Cursor.Current = Cursors.WaitCursor;
            ucReturn ucReturn = new ucReturn();
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(ucReturn);
            ucReturn.Dock = System.Windows.Forms.DockStyle.Fill;
            ucReturn.Location = new System.Drawing.Point(2, 2);
            ucReturn.Name = "ucReturn";
            ucReturn.TabIndex = 0;
            this.gridView = ucReturn.GridView;
            Cursor.Current = Cursors.Default;

        }

        void bbItemBroken_ItemClick(object sender, ItemClickEventArgs e)
        {

            Cursor.Current = Cursors.WaitCursor;
            ucBroken ucBroken = new ucBroken();
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(ucBroken);
            ucBroken.Dock = System.Windows.Forms.DockStyle.Fill;
            ucBroken.Location = new System.Drawing.Point(2, 2);
            ucBroken.Name = "ucBroken";
            ucBroken.TabIndex = 0;
            this.gridView = ucBroken.GridView;
            Cursor.Current = Cursors.Default;

        }

        void bbItemBorrow_ItemClick(object sender, ItemClickEventArgs e)
        {

            Cursor.Current = Cursors.WaitCursor;
            ucBorrow ucBorrow = new ucBorrow();
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(ucBorrow);
            ucBorrow.Dock = System.Windows.Forms.DockStyle.Fill;
            ucBorrow.Location = new System.Drawing.Point(2, 2);
            ucBorrow.Name = "ucBorrow";
            ucBorrow.TabIndex = 0;
            this.gridView = ucBorrow.GridView;

            Cursor.Current = Cursors.Default;
        }

        void bbItemJustnew_ItemClick(object sender, ItemClickEventArgs e)
        {

            Cursor.Current = Cursors.WaitCursor;
            ucJustNew ucJustNew = new ucJustNew();
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(ucJustNew);
            ucJustNew.Dock = System.Windows.Forms.DockStyle.Fill;
            ucJustNew.Location = new System.Drawing.Point(2, 2);
            ucJustNew.Name = "ucJustNew";
            ucJustNew.TabIndex = 0;
            this.gridView = ucJustNew.GridView;
            Cursor.Current = Cursors.Default;
        }

        void bbItemImport_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucImport ucImport = new ucImport();
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(ucImport);
            // 
            // ucImport
            // 
            ucImport.Dock = System.Windows.Forms.DockStyle.Fill;
            ucImport.Location = new System.Drawing.Point(2, 2);
            ucImport.Name = "ucImport";
            ucImport.TabIndex = 0;
            this.gridView = ucImport.GridView;
            Cursor.Current = Cursors.Default;
        }

        ///
        /// Product Query
        /// 

        private void subBCThoiGianLamViecthang_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucBCTimeMonth ucBcTimeMonth = new ucBCTimeMonth();
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(ucBcTimeMonth);
            // 
            // ucBcTimeMonth
            // 
            ucBcTimeMonth.Dock = System.Windows.Forms.DockStyle.Fill;
            ucBcTimeMonth.Location = new System.Drawing.Point(2, 2);
            ucBcTimeMonth.Name = "ucBcTimeMonth";
            ucBcTimeMonth.TabIndex = 0;
            Cursor.Current = Cursors.Default;
        }

        private void subNGNoibo_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucNGNoibo ucNgNoiBo = new ucNGNoibo();
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(ucNgNoiBo);
            // 
            // ucNgNoiBo
            // 
            ucNgNoiBo.Dock = System.Windows.Forms.DockStyle.Fill;
            ucNgNoiBo.Location = new System.Drawing.Point(2, 2);
            ucNgNoiBo.Name = "ucNgNoiBo";
            ucNgNoiBo.TabIndex = 0;
            Cursor.Current = Cursors.Default;
        }


        private void subCheckThoiGianLamViec_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucCheckWorkTime ucCheck = new ucCheckWorkTime();
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(ucCheck);
            // 
            // ucNgNoiBo
            // 
            ucCheck.Dock = System.Windows.Forms.DockStyle.Fill;
            ucCheck.Location = new System.Drawing.Point(2, 2);
            ucCheck.Name = "ucCheck";
            ucCheck.TabIndex = 0;
            Cursor.Current = Cursors.Default;
        }


        private void bbtBcKeHoach_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucBaoCaoKeHoach ucBcKh = new ucBaoCaoKeHoach();
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(ucBcKh);
            // 
            // ucNgNoiBo
            // 
            ucBcKh.Dock = System.Windows.Forms.DockStyle.Fill;
            ucBcKh.Location = new System.Drawing.Point(2, 2);
            ucBcKh.Name = "ucBcKh";
            ucBcKh.TabIndex = 0;
            Cursor.Current = Cursors.Default;
        }
        #endregion

        #region order
        private void bbtOrderProduct_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucOrderMain uc = new ucOrderMain();
            uc.BindaData(OrderConstants.Configuration.ReportName.QTheoDoiDatHang, bbtTheoDoiDatHang.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(uc);
            // 
            // ucNgNoiBo
            // 
            uc.Dock = System.Windows.Forms.DockStyle.Fill;
            uc.Location = new System.Drawing.Point(2, 2);
            uc.Name = "QTheoDoiDatHang";
            uc.TabIndex = 0;
            Cursor.Current = Cursors.Default;
        }

        private void bbtHangchuave_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucOrderMain uc = new ucOrderMain();
            uc.BindaData(OrderConstants.Configuration.ReportName.QHangchuave, bbtHangchuave.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(uc);
            // 
            // ucNgNoiBo
            // 
            uc.Dock = System.Windows.Forms.DockStyle.Fill;
            uc.Location = new System.Drawing.Point(2, 2);
            uc.Name = "QHangchuave";
            uc.TabIndex = 0;
            Cursor.Current = Cursors.Default;
        }

        private void bbtDatHangDetails_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucOrderMain uc = new ucOrderMain();
            uc.BindaData(OrderConstants.Configuration.ReportName.QDatHangDetails, bbtDatHangDetails.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(uc);
            // 
            // ucNgNoiBo
            // 
            uc.Dock = System.Windows.Forms.DockStyle.Fill;
            uc.Location = new System.Drawing.Point(2, 2);
            uc.Name = "QHangchuave";
            uc.TabIndex = 0;
            Cursor.Current = Cursors.Default;
        }

        private void bbtMonthlyThanhToan_hang_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucOrderMain uc = new ucOrderMain();
            uc.BindaData(OrderConstants.Configuration.ReportName.QMonthlyThanhToan_hang, bbtMonthlyThanhToan_hang.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(uc);
            // 
            // ucNgNoiBo
            // 
            uc.Dock = System.Windows.Forms.DockStyle.Fill;
            uc.Location = new System.Drawing.Point(2, 2);
            uc.Name = "QDatHangDetails";
            uc.TabIndex = 0;
            Cursor.Current = Cursors.Default;
        }

        private void bbtMonthlyDatHangBophanDetails_Crosstab_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucOrderMain uc = new ucOrderMain();
            uc.BindaData(OrderConstants.Configuration.ReportName.QMonthlyDatHangBophanDetails_Crosstab, bbtMonthlyDatHangBophanDetails_Crosstab.Caption);
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(uc);
            // 
            // ucNgNoiBo
            // 
            uc.Dock = System.Windows.Forms.DockStyle.Fill;
            uc.Location = new System.Drawing.Point(2, 2);
            uc.Name = "QMonthlyThanhToan_hang";
            uc.TabIndex = 0;
            Cursor.Current = Cursors.Default;
        }

        private void bbtPrint_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucOrderMain uc = new ucOrderMain();
            uc.Print();
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(uc);
            // 
            // ucOrderMain
            // 
            uc.Dock = System.Windows.Forms.DockStyle.Fill;
            uc.Location = new System.Drawing.Point(2, 2);
            uc.Name = "QMonthlyDatHangBophanDetails_Crosstab";
            //uc.TabIndex = 0;
            Cursor.Current = Cursors.Default;
        }

        private void bbtOrderData_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucOrderMain uc = new ucOrderMain();
            uc.GetAllOrderData();
            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(uc);
            // 
            // OrderData
            // 
            uc.Dock = System.Windows.Forms.DockStyle.Fill;
            uc.Location = new System.Drawing.Point(2, 2);
            uc.Name = "OrderData";
            //uc.TabIndex = 0;
            Cursor.Current = Cursors.Default;
        }
        #endregion

        #region Permission
        private void userList_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucUser users = new ucUser();
            this.basiclistPanel.LoadControl(users);
            users.Dock = DockStyle.Fill;
            Cursor.Current = Cursors.Default;

        }

        private void userGroup_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucUserGroup group = new ucUserGroup();
            this.basiclistPanel.LoadControl(group);
            group.Dock = DockStyle.Fill;
            Cursor.Current = Cursors.Default;
        }

        private void barPermission_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucPermission permission = new ucPermission();
            this.basiclistPanel.LoadControl(permission);
            permission.Dock = DockStyle.Fill;
            Cursor.Current = Cursors.Default;
        }

        private void barFunction_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucFunctions function = new ucFunctions();
            this.basiclistPanel.LoadControl(function);
            function.Dock = DockStyle.Fill;
            Cursor.Current = Cursors.Default;
        }

        private void barButtonItem44_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            //write Log
            log.Table = LogConstant.Table.SystemAdmin.Users;
            log.Task = LogConstant.Task.SignOut;
            log.Description = this._userName + " logged out successfully."; ;
            log.WriteLog();

            frm_Login login_Form = new frm_Login();
            login_Form.ShowDialog();
            if (role.Is_Login())
            {
                systemLoad = false;
                CallSystemPanel();
                InitToolControls();
                ShowHideLink();
            }
            else
            {
                this.Dispose();
            }
            Cursor.Current = Cursors.Default;
        }
        #endregion


        ///////////////////////////////////////////
        /// Check role
        ///////////////////////////////////////////
        private void ShowHideLink()
        {
            //if (role.HasNotRight(PermissionConstants.Functions.Tab.System))
            //    systemTab.Visible = false;
            //role.Is_Login();

            barStaticUserName.Caption = role.UserName;

            //Enable item is hasNoRight
            if (role.HasNotRight(PermissionConstants.Functions.Tab.Business))
                businessTab.Visible = false;

            if (role.HasNotRight(PermissionConstants.Functions.Tab.Assembly))
                assemblyTab.Visible = false;

            if (role.HasNotRight(PermissionConstants.Functions.Tab.Product))
                rpProduct.Visible = false;

            if (role.HasNotRight(PermissionConstants.Functions.Tab.ToolCutting))
                rpCatProducTool.Visible = false;

            if (role.HasNotRight(PermissionConstants.Functions.Tab.F2Order))
                rpCatOrder.Visible = false;

            if (role.HasNotRight(PermissionConstants.Functions.General.UserGroups))
                userGroup.Visibility = BarItemVisibility.Never;
            //userGroup.Enabled = false;

            if (role.HasNotRight(PermissionConstants.Functions.General.Users))
                userList.Visibility = BarItemVisibility.Never;

            if (role.HasNotRight(PermissionConstants.Functions.General.Fuctions))
                barFunction.Visibility = BarItemVisibility.Never;

            if (role.HasNotRight(PermissionConstants.Functions.General.Permission))
                barPermission.Visibility = BarItemVisibility.Never;

            if (role.HasNotRight(PermissionConstants.Functions.General.Log))
                barLog.Visibility = BarItemVisibility.Never;

            if (role.HasNotRight(PermissionConstants.Functions.SystemAdmin.BackUpDB))
                databaseBackup.Visibility = BarItemVisibility.Never;

            if (role.HasNotRight(PermissionConstants.Functions.SystemAdmin.RestoreDB))
                databaseRestore.Visibility = BarItemVisibility.Never;

            //Enable item is hasRight
            if (role.HasRight(PermissionConstants.Functions.Tab.Business))
                businessTab.Visible = true;

            if (role.HasRight(PermissionConstants.Functions.Tab.Assembly))
                assemblyTab.Visible = true;

            if (role.HasRight(PermissionConstants.Functions.Tab.Product))
                rpProduct.Visible = true;

            //if (role.HasRight(PermissionConstants.Functions.Tab.ToolCutting))
            //    rpCatProducTool.Visible = true;

            //if (role.HasRight(PermissionConstants.Functions.Tab.F2Order))
            //    rpCatOrder.Visible = true;

            if (role.HasRight(PermissionConstants.Functions.General.UserGroups))
                userGroup.Visibility = BarItemVisibility.Always;

            if (role.HasRight(PermissionConstants.Functions.General.Users))
                userList.Visibility = BarItemVisibility.Always;

            if (role.HasRight(PermissionConstants.Functions.General.Fuctions))
                barFunction.Visibility = BarItemVisibility.Always;

            if (role.HasRight(PermissionConstants.Functions.General.Permission))
                barPermission.Visibility = BarItemVisibility.Always;

            if (role.HasRight(PermissionConstants.Functions.General.Log))
                barLog.Visibility = BarItemVisibility.Always;

            if (role.HasRight(PermissionConstants.Functions.SystemAdmin.BackUpDB))
                databaseBackup.Visibility = BarItemVisibility.Always;

            if (role.HasRight(PermissionConstants.Functions.SystemAdmin.RestoreDB))
                databaseRestore.Visibility = BarItemVisibility.Always;

        }

        private void databaseBackup_ItemClick(object sender, ItemClickEventArgs e)
        {
            ModuleWaitDialog.CreateWaitDialog("", "Please waiting...");
            ModuleWaitDialog.SetWaitDialogCaption("Backing up database...");
            try
            {
                DateTime date = DateTime.Now;
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "BACKUP DATABASE [rrc_database]"
                    + " TO DISK = N'C:\\BackupDatabase\\rrc_database" + date.ToString("yyyyMMddHHmmss") + ".bak'"
                    + " WITH DESCRIPTION = N'Database: rrc_database', NOFORMAT,"
                    + " NOINIT, MEDIADESCRIPTION = N'Disk', NOSKIP, REWIND, NOUNLOAD, STATS=10";
                sql_cmd.CommandTimeout = 30000;
                sql_cmd.ExecuteNonQuery();
                myConnection.Close();
                MessageBox.Show("Database has been backup succeedfully.");
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot backup database. Error: " + ex.Message);
            }
            ModuleWaitDialog.CloseWaitDialog();
        }

        #region User task

        private void barBtAccount_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            Form info = new frm_UserInfo();
            info.ShowDialog();
            Cursor.Current = Cursors.Default;
        }

        private void barBtChangePwd_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            Form changePwd = new frm_ChangePass();
            changePwd.ShowDialog();
            Cursor.Current = Cursors.Default;
        }

        private void barBtLogout_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            //write Log
            log.Table = LogConstant.Table.SystemAdmin.Users;
            log.Task = LogConstant.Task.SignOut;
            log.Description = this._userName + " logged out successfully."; ;
            log.WriteLog();

            frm_Login login_Form = new frm_Login();
            login_Form.ShowDialog();
            if (role.Is_Login())
            {
                systemLoad = false;
                CallSystemPanel();
                InitToolControls();
                ShowHideLink();
            }
            else
            {
                this.Dispose();
            }
            Cursor.Current = Cursors.Default;
        }

        private void barLog_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ucLogs log = new ucLogs();
            this.basiclistPanel.LoadControl(log);
            log.Dock = DockStyle.Fill;
            Cursor.Current = Cursors.Default;
        }
        private void btInfomation_ItemClick(object sender, ItemClickEventArgs e)
        {
            var frm = new frmAbout();
            frm.Show();
        }
        #endregion

        #region Theme Styles
        private void barChangeSkin_Popup(object sender, EventArgs e)
        {
            foreach (BarItemLink link in barChangeSkin.ItemLinks)
            {
                if (link.Item.Caption == defaultLookAndFeel1.LookAndFeel.ActiveSkinName)
                    ((BarCheckItem)link.Item).Checked = true;
                else
                    ((BarCheckItem)link.Item).Checked = false;

            }
        }

        private void AddStyleList()
        {
            SkinContainerCollection skins = SkinManager.Default.Skins;
            BarCheckItem item;
            for (int i = 0; i < skins.Count; i++)
            {
                item = ribbonControl1.Items.CreateCheckItem(skins[i].SkinName, false);
                item.Tag = skins[i].SkinName;
                barChangeSkin.ItemLinks.Add(item);
                item.ItemClick += item_ItemClick;
            }

            try
            {

                if (!File.Exists(PermissionConstants.ConfigFile))
                {
                    ERP_RORZE_PERMISSION.Base.Utilities.WriteToFile(PermissionConstants.ConfigFile, defaultLookAndFeel1.LookAndFeel.ActiveSkinName + "\n" + "None");
                }
                else
                {
                    string skin = ERP_RORZE_PERMISSION.Base.Utilities.ReadFromFile(PermissionConstants.ConfigFile)[0];
                    bool flag = false;
                    foreach (BarItemLink itemLink in barChangeSkin.ItemLinks)
                    {
                        if (skin.Equals(itemLink.Item.Caption))
                        {
                            defaultLookAndFeel1.LookAndFeel.SetSkinStyle(skin);
                            flag = true;
                            break;
                        }

                    }

                    if (!flag)
                    {
                        defaultLookAndFeel1.LookAndFeel.SetSkinStyle(defaultLookAndFeel1.LookAndFeel.ActiveSkinName);
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        void item_ItemClick(object sender, ItemClickEventArgs e)
        {
            defaultLookAndFeel1.LookAndFeel.SetSkinStyle(e.Item.Tag.ToString());
            ERP_RORZE_PERMISSION.Base.Utilities.WriteToFile(PermissionConstants.ConfigFile, e.Item.Tag + "\n" + "None");

        }

        #endregion
    }
}
